home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / comm1 / qwesorcs.lha / qwe_sources / Flut!v4 / Flut-Server.c < prev    next >
Text File  |  1996-03-24  |  11KB  |  579 lines

  1. const char *_VERSION="\0$VER: Flut-Server! 4.0a (23.03.96)";
  2.  
  3. #include    <dos/dos.h>
  4. #include    "Flut.h"
  5.  
  6. #define gamespfad    0
  7. #define scorepfad    1
  8. #define newsdatpfad    2
  9. #define halldatpfad    3
  10.  
  11. void user_points(UBYTE anforder);
  12. void killgame(UBYTE welches);
  13. void hall_add(UBYTE art, UBYTE pos, UBYTE anforder);
  14. void pointeradd(void);
  15. void events(void);
  16. void save_hall(void);
  17. void load_hall(void);
  18. void save_news(void);
  19. void load_news(void);
  20. void save_games(void);
  21. void load_games(void);
  22. void save_scores(void);
  23. void load_scores(void);
  24. void news_add(void);
  25. void scores_add(void);
  26. void make_path(byte pathnr);
  27.  
  28. struct allstruct *all;
  29. struct tm*datum_zeit;
  30.  
  31. ULONG    today;
  32.  
  33. FILE    *fp;
  34.  
  35. char    path[4][17]={    "flut.games",    "flut.scores",    "flut.news",    "flut.hall1"},
  36.         pathstart[108],
  37.         pathwork[108],
  38.  
  39.         line_c[200],*p3,
  40.         line_d[200],*p4;
  41.  
  42. int        wertung_a,wertung_b;
  43.  
  44. BPTR    lock;
  45.  
  46. UBYTE    x,y,i,j,k;
  47.  
  48. BOOL    off,hall_yes;
  49.  
  50. UBYTE    tasks;
  51.  
  52. main()
  53. {
  54.     if (flutport=CreatePort(FLUT_PORT,0))
  55.     {
  56.         if(all=(struct allstruct *)AllocVec(sizeof(struct allstruct),MEMF_PUBLIC|MEMF_CLEAR))
  57.         {
  58.             lock=GetProgramDir();
  59.             NameFromLock(lock,pathstart,108);
  60.  
  61.             load_games();
  62.             load_scores();
  63.             load_news();
  64.             load_hall();
  65.  
  66.             while(!off)
  67.             {
  68.                 WaitPort(flutport);
  69.                 while(ptr=(struct messagestruct *)GetMsg(flutport))
  70.                 {
  71.                     if(off)
  72.                     {
  73.                         Permit();
  74.                         off=FALSE;
  75.                     }
  76.  
  77.                     events();
  78.  
  79.                     if(tasks==0)
  80.                     {
  81.                         off=TRUE;
  82.                         Forbid();
  83.                     }
  84.                 }
  85.             }
  86.             FreeVec(all);
  87.         }
  88.         DeletePort(flutport);
  89.         Permit();
  90.     }
  91. }
  92.  
  93. void make_path(byte pathnr)
  94. {
  95.     strcpy(pathwork,pathstart);
  96.     AddPart(pathwork,path[pathnr],108);
  97. }
  98.  
  99. void pointeradd(void)
  100. {
  101.     if(all->pointer==9)
  102.     {
  103.         all->pointer=0;
  104.     }
  105.     else
  106.     {
  107.         all->pointer++;
  108.     }
  109.     ptr->pointer=all->pointer;
  110. }
  111.  
  112. void killgame(UBYTE welches)
  113. {
  114.     for(j=0;j<all->game[welches].y_size;j++)
  115.     {
  116.         for(i=0;i<all->game[welches].x_size;i++)
  117.         {
  118.             all->game[welches].farbe[i][j]=0;
  119.             all->game[welches].fuellgrad[i][j]=0;
  120.         }
  121.     }
  122.     for(i=0;i<6;i++)
  123.     {
  124.         all->game[welches].player[i].userid=0;
  125.     }
  126.     all->game[welches].anzahl=0;
  127.     all->game[welches].x_size=0;
  128.     all->game[welches].y_size=0;
  129. }
  130.  
  131. void events(void)
  132. {
  133.     switch (ptr->com)
  134.     {
  135.         case MSG_GAME:
  136.             ptr->result=FALSE;
  137.             if(ptr->game_neu<0)
  138.             {
  139.                 ptr->result=TRUE;
  140.             }
  141.             else
  142.             {
  143.                 if(ptr->game_neu>49)
  144.                 {
  145.                     if(!all->game[ptr->game_neu].belegt || all->olg[ptr->game_neu-50].joined>0)
  146.                     {
  147.                         ptr->result=TRUE;
  148.                         all->game[ptr->game_neu].belegt=TRUE;
  149.                         if(all->olg[ptr->game_neu-50].joined>0)
  150.                         {
  151.                             all->olg[ptr->game_neu-50].joined++;
  152.                             ptr->olgpointer=all->olg[ptr->game_neu-50].pointer;
  153.                         }
  154.                     }
  155.                 }
  156.                 else
  157.                 {
  158.                     if(!all->game[ptr->game_neu].belegt)
  159.                     {
  160.                         ptr->result=TRUE;
  161.                         all->game[ptr->game_neu].belegt=TRUE;
  162.                     }
  163.                 }
  164.             }
  165.             if(ptr->result)
  166.             {
  167.                 if(ptr->game_alt>49)
  168.                 {
  169.                     if(all->olg[ptr->game_alt-50].joined>0)
  170.                     {
  171.                         all->olg[ptr->game_alt-50].joined--;
  172.                     }
  173.                     if(all->olg[ptr->game_alt-50].joined==0)
  174.                     {
  175.                         all->game[ptr->game_alt].belegt=FALSE;
  176.                         killgame(ptr->game_alt);
  177.                     }
  178.                 }
  179.                 else
  180.                 {
  181.                     all->game[ptr->game_alt].belegt=FALSE;
  182.                 }
  183.             }
  184.             break;
  185.         case MSG_SCORES:
  186.             scores_add();
  187.             break;
  188.         case MSG_INIT:
  189.             ptr->all=all;
  190.             tasks++;
  191.             sprintf(all->mails[all->pointer],"%s joined Flut!",ptr->pname);
  192.             pointeradd();
  193.             break;
  194.         case MSG_QUIT:
  195.             tasks--;
  196.  
  197.             if(tasks==0)
  198.             {
  199.                 save_games();
  200.                 save_scores();
  201.                 save_news();
  202.                 save_hall();
  203.             }
  204.             else
  205.             {
  206.                 if(ptr->timeleft==0)
  207.                 {
  208.                     sprintf(all->mails[all->pointer],"%s left the BBS because of CARRIER/TIME!",ptr->pname);
  209.                 }
  210.                 else
  211.                 {
  212.                     sprintf(all->mails[all->pointer],"%s left Flut!",ptr->pname);
  213.                 }
  214.                 pointeradd();
  215.             }
  216.             break;
  217.         case MSG_MESS:
  218.             sprintf(all->mails[all->pointer],"%s: %s",ptr->pname,ptr->mess);
  219.             pointeradd();
  220.             break;
  221.     }
  222.     ReplyMsg(ptr);
  223. }
  224.  
  225. void save_hall(void)
  226. {
  227.     for(i=0;i<2;i++)
  228.     {
  229.         path[halldatpfad][9]=i+49;
  230.         make_path(halldatpfad);
  231.         if(fp=fopen(pathwork,"w"))
  232.         {
  233.             for(j=0;j<10;j++)
  234.             {
  235.                 fputs(all->fame.hall[i][j],fp);
  236.             }
  237.             fclose(fp);
  238.         }
  239.     }
  240. }
  241.  
  242. void load_hall(void)
  243. {
  244.     for(i=0;i<2;i++)
  245.     {
  246.         path[halldatpfad][9]=i+49;
  247.  
  248.         j=0;
  249.         make_path(halldatpfad);
  250.         if(fp=fopen(pathwork,"r"))
  251.         {
  252.             while (fgets(all->fame.hall[i][j],sizeof(all->fame.hall[i][0]),fp))
  253.             {
  254.                 j++;
  255.             }
  256.             fclose(fp);
  257.         }
  258.     }
  259. }
  260.  
  261. void save_news(void)
  262. {
  263.     make_path(newsdatpfad);
  264.     if(fp=fopen(pathwork,"w"))
  265.     {
  266.         for(j=0;j<15;j++)
  267.         {
  268.             fputs(all->fame.news[j],fp);
  269.         }
  270.         fclose(fp);
  271.     }
  272. }
  273.  
  274. void load_news(void)
  275. {
  276.     i=0;
  277.     make_path(newsdatpfad);
  278.     if(fp=fopen(pathwork,"r"))
  279.     {
  280.         while (fgets(all->fame.news[i],sizeof(all->fame.news[0]),fp))
  281.         {
  282.             i++;
  283.         }
  284.         fclose(fp);
  285.     }
  286. }
  287.  
  288. void save_games(void)
  289. {
  290.     make_path(gamespfad);
  291.     if (fp=fopen(pathwork,"w"))
  292.     {
  293.         for(x=0;x<50;x++)
  294.         {
  295.             if(all->game[x].anzahl>0)
  296.             {
  297.                 sprintf(line_c,"%d;%2.2d*%2.2d;%d;%d\n",all->game[x].anzahl,all->game[x].x_size,all->game[x].y_size,all->game[x].status,all->game[x].border);
  298.                 fputs(line_c,fp);
  299.  
  300.                 k=0;
  301.                 for(j=0;j<all->game[x].y_size;j++)
  302.                 {
  303.                     for(i=0;i<all->game[x].x_size;i++)
  304.                     {
  305.                         line_c[k]=all->game[x].farbe[i][j]+48;
  306.                         line_d[k]=all->game[x].fuellgrad[i][j]+48;
  307.                         k++;
  308.                     }
  309.                 }
  310.                 line_c[k]=10;
  311.                 line_d[k]=10;
  312.                 line_c[k+1]=0;
  313.                 line_d[k+1]=0;
  314.  
  315.                 fputs(line_c,fp);
  316.                 fputs(line_d,fp);
  317.  
  318.                 for(y=0;y<all->game[x].anzahl;y++)
  319.                 {
  320.                     sprintf(line_c,"%9.9d%c\n",all->game[x].player[y].userid,all->game[x].player[y].last_wall_lese);
  321.                     fputs(line_c,fp);
  322.                     sprintf(line_c,"%10.10d\n",all->game[x].player[y].last_move);
  323.                     fputs(line_c,fp);
  324.                 }
  325.             }
  326.             else
  327.             {
  328.                 strcpy(line_c,"0\n");
  329.                 fputs(line_c,fp);
  330.             }
  331.         }
  332.         fclose(fp);
  333.     }
  334. }
  335.  
  336.  
  337. void load_games(void)
  338. {
  339.     x=0;
  340.     make_path(gamespfad);
  341.     if (fp=fopen(pathwork,"r"))
  342.     {
  343.         while (fgets(line_c,sizeof(line_c),fp))
  344.         {
  345.             all->game[x].anzahl=line_c[0]-48;
  346.             if(all->game[x].anzahl>0)
  347.             {
  348.                 all->game[x].x_size=atoi(line_c + 2);
  349.                 all->game[x].y_size=atoi(line_c + 5);
  350.                 all->game[x].status=atoi(line_c + 8);
  351.                 all->game[x].border=atoi(line_c + 10);
  352.                 fgets(line_c,sizeof(line_c),fp);
  353.                 fgets(line_d,sizeof(line_d),fp);
  354.                 k=0;
  355.                 for(j=0;j<all->game[x].y_size;j++)
  356.                 {
  357.                     for(i=0;i<all->game[x].x_size;i++)
  358.                     {
  359.                         all->game[x].farbe[i][j]=line_c[k]-48;
  360.                         all->game[x].fuellgrad[i][j]=line_d[k]-48;
  361.                         k++;
  362.                     }
  363.                 }
  364.                 y=0;
  365.                 while(y<all->game[x].anzahl)
  366.                 {
  367.                     fgets(line_c,sizeof(line_c),fp);
  368.                     all->game[x].player[y].userid=atoi(line_c);
  369.                     all->game[x].player[y].last_wall_lese=line_c[9];
  370.                     fgets(line_c,sizeof(line_c),fp);
  371.                     all->game[x].player[y].last_move=atol(line_c);
  372.                     y++;
  373.                 }
  374.             }
  375.             x++;
  376.         }
  377.         fclose(fp);
  378.     }
  379. }
  380.  
  381. void save_scores(void)
  382. {
  383.     make_path(scorepfad);
  384.     if (fp=fopen(pathwork,"w"))
  385.     {
  386.         x=0;
  387.         while(all->user[x].userid!=0)
  388.         {
  389.             if(all->user[x].points[0]<0)
  390.             {
  391.                 sprintf(line_c,"%9.9d#%4.4d#%d\n",all->user[x].userid,all->user[x].points[0],all->user[x].points[1]);
  392.             }
  393.             else
  394.             {
  395.                 sprintf(line_c,"%9.9d#%5.5d#%d\n",all->user[x].userid,all->user[x].points[0],all->user[x].points[1]);
  396.             }
  397.             fputs(line_c,fp);
  398.             x++;
  399.         }
  400.         fclose(fp);
  401.     }
  402. }
  403.  
  404.  
  405. void load_scores(void)
  406. {
  407.     make_path(scorepfad);
  408.     if (fp=fopen(pathwork,"r"))
  409.     {
  410.         x=0;
  411.         while (fgets(line_c,sizeof(line_c),fp))
  412.         {
  413.             all->user[x].userid=atoi(line_c);
  414.             all->user[x].points[0]=atoi(line_c+10);
  415.             all->user[x].points[1]=atoi(line_c+16);
  416.             x++;
  417.         }
  418.         fclose(fp);
  419.     }
  420. }
  421.  
  422. void hall_add(UBYTE art, UBYTE pos, UBYTE anforder)
  423. {
  424.     datum_zeit=localtime(&today);
  425.     strftime(line_c,10,"%d-%b-%y",datum_zeit);
  426.     for(x=9;x>0;x--)
  427.     {
  428.         strcpy(all->fame.hall[art][x],all->fame.hall[art][x-1]);
  429.     }
  430.     sprintf(all->fame.hall[art][0],"%dC%s %s\n",pos,line_c,ptr->name[anforder]);
  431. }
  432.  
  433. void news_add(void)
  434. {
  435.     datum_zeit=localtime(&today);
  436.     strftime(line_c,10,"%d-%b-%y",datum_zeit);
  437.     for(x=14;x>0;x--)
  438.     {
  439.         strcpy(all->fame.news[x],all->fame.news[x-1]);
  440.     }
  441.     if(ptr->game_neu>49)
  442.     {
  443.         sprintf(all->fame.news[0],"C3%s  OLG ---/--- C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s\n",line_c,ptr->status[0],ptr->name[0],ptr->status[1],ptr->name[1],ptr->status[2],ptr->name[2],ptr->status[3],ptr->name[3],ptr->status[4],ptr->name[4],ptr->status[5],ptr->name[5]);
  444.     }
  445.     else
  446.     {
  447.         sprintf(all->fame.news[0],"C3%s   %2.2d %3.3d/%2.2d C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s C%1x%-8.8s\n",line_c,ptr->game_neu+1,wertung_a,wertung_b,ptr->status[0],ptr->name[0],ptr->status[1],ptr->name[1],ptr->status[2],ptr->name[2],ptr->status[3],ptr->name[3],ptr->status[4],ptr->name[4],ptr->status[5],ptr->name[5]);
  448.     }
  449. }
  450.  
  451. void user_points(UBYTE anforder)
  452. {
  453.     x=0;
  454.     while(all->user[x].userid!=all->game[ptr->game_neu].player[anforder].userid && all->user[x].userid!=0)
  455.     {
  456.         x++;
  457.     }
  458.     all->user[x].userid=all->game[ptr->game_neu].player[anforder].userid;
  459.  
  460.     if(ptr->status[anforder]==1)
  461.     {
  462.         all->user[x].points[0]+=wertung_a;
  463.     }
  464.     if(ptr->status[anforder]==2)
  465.     {
  466.         all->user[x].points[0]+=wertung_b;
  467.     }
  468.     if(anforder==ptr->akt_user-1)
  469.     {
  470.         if(ptr->game_neu>49)
  471.         {
  472.             all->user[x].points[1]+=ptr->punkte;
  473.         }
  474.         else
  475.         {
  476.             all->user[x].points[0]+=ptr->punkte;
  477.         }
  478.     }
  479.  
  480.     if(all->user[x].points[0] > 999)
  481.     {
  482.         hall_add(0,7,anforder);
  483.         hall_yes=TRUE;
  484.     }
  485.     if(all->user[x].points[1] > 999)
  486.     {
  487.         hall_add(1,43,anforder);
  488.         hall_yes=TRUE;
  489.     }
  490. }
  491.  
  492. void scores_add(void)
  493. {
  494.     ptr->result=FALSE;
  495.     if(ptr->game_neu>49)
  496.     {
  497.         for(y=0;y<all->game[ptr->game_neu].anzahl;y++)
  498.         {
  499.             ptr->all->olg[ptr->game_neu-50].status[y]=ptr->status[y];
  500.         }
  501.     }
  502.  
  503.     time(&today);
  504.  
  505.     hall_yes=FALSE;
  506.  
  507.     if(ptr->game_neu>49)
  508.     {
  509.         wertung_a=0;
  510.         wertung_b=0;
  511.     }
  512.     else
  513.     {
  514.         wertung_a=all->game[ptr->game_neu].x_size*ptr->all->game[ptr->game_neu].y_size*ptr->all->game[ptr->game_neu].anzahl/6;
  515.         wertung_b=all->game[ptr->game_neu].x_size*ptr->all->game[ptr->game_neu].y_size/(-6);
  516.     }
  517.  
  518.     if(ptr->status[y]==0)
  519.     {
  520.         user_points(ptr->akt_user-1);
  521.     }
  522.     else
  523.     {
  524.         for(y=0;y<all->game[ptr->game_neu].anzahl;y++)
  525.         {
  526.             user_points(y);
  527.         }
  528.     }
  529.  
  530.     if(ptr->status[0]>0)
  531.     {
  532.         news_add();
  533.     }
  534.  
  535.     if(hall_yes)
  536.     {
  537.         i=0;
  538.         j=0;
  539.         while(all->user[j].userid>0)
  540.         {
  541.             if(ptr->game_neu>49)
  542.             {
  543.                 all->user[j].points[1]=0;
  544.             }
  545.             else
  546.             {
  547.                 all->user[j].points[0]=0;
  548.             }
  549.  
  550.             if(all->user[j].points[0]==0 && all->user[j].points[1]==0)
  551.             {
  552.                 all->user[j].userid=0;
  553.             }
  554.  
  555.             if(all->user[i].userid==0 && all->user[j].userid!=0)
  556.             {
  557.                 all->user[i].userid=all->user[j].userid;
  558.                 all->user[i].points[0]=all->user[j].points[0];
  559.                 all->user[i].points[1]=all->user[j].points[1];
  560.                 all->user[j].userid=0;
  561.                 all->user[j].points[0]=0;
  562.                 all->user[j].points[1]=0;
  563.             }
  564.  
  565.             if(all->user[i].userid!=0)
  566.             {
  567.                 i++;
  568.             }
  569.  
  570.             j++;
  571.         }
  572.         ptr->result=TRUE;
  573.     }
  574.     else
  575.     {
  576.         ptr->result=FALSE;
  577.     }
  578. }
  579.